package com.ts.manager;

import android.view.KeyEvent;

import com.rio.core.L;
import com.rio.core.U;
import com.rio.helper.Sleeper;
import com.rio.layout.TaskManager;
import com.rio.layout.view.SimpleTask;
import com.ts.host.APP;
import com.ts.host.Event;
import com.ts.host.N;
import com.ts.host.TToast;

import de.greenrobot.event.EventBus;

public class WaterManager {

	private WaterTask mColdWaterTask;

	private WaterTask mWarmWaterTask;

	private ChargingTask mChargingTask;

	private CountDownTask mCountDownTask;

	private boolean mChargingStart;
	
	private int mVolume = 0;

	private int mMode = 0;

	public static final int MODE_FREE = 3;
	public static final int MODE_CHARGING = 1;
	
	private static final int CANCEL_TIME = 200;//20秒倒计时

	private static WaterManager mInstance;

	public static WaterManager getInstance() {
		if (U.isNull(mInstance)) {
			mInstance = new WaterManager();
		}
		return mInstance;
	}

	public int getMode() {
		return mMode;
	}
	public boolean listen(int keyCode) {
		if (mMode == MODE_FREE) {
			switch (keyCode) {

			case KeyEvent.KEYCODE_VOLUME_DOWN:

				getColdWaterTask().handle(keyCode);

				return true;

			case KeyEvent.KEYCODE_VOLUME_UP:

				getWarmWaterTask().handle(keyCode);
				return true;

			}

		}

		if (mChargingStart && mMode == MODE_CHARGING) {
			getChargingTask().handle(keyCode);
		}

		// 截获音量进度条
		if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
				|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			return true;
		}

		return false;
	}

	public void startCharging(int volume) {
		if (!mChargingStart && mMode == MODE_CHARGING) {
			setVolume(volume);
			mChargingStart = true;
			// 开始10秒倒计时
			getCountDownTask().start();
		}
	}

	private void setVolume(int volume) {
		int time = WaterTask.WATER_DEFAULT;
		if(volume <= 250){
			time = 12;
		}else if (volume <= 500){
			time = 24;
		}else {
			time = 36;
		}
		mVolume = volume;
		getColdWaterTask().setSecond(time);
		getWarmWaterTask().setSecond(time);
		getChargingTask().setSecond(time);
	}

	/**
	 * 是否转换模式
	 * @param mode
	 */
	public boolean setMode(int mode) {
		boolean resutl = mMode != mode;
		if(resutl){
			mMode = mode;
			TaskManager.getInstance().async(new SimpleTask() {
				
				@Override
				public  Object onBGThread(Object... params) throws Exception {
					L.i("mode: " +mMode);
					mChargingStart = false;
					getColdWaterTask().reset();
					getWarmWaterTask().reset();
					getChargingTask().reset();
					getCountDownTask().reset();
					setVolume(220);
					N.closeAll();
					return null;
				}
				
				@Override
				public void onUIThread(Object data, Object... params)
						throws Exception {
						switch (mMode) {
						case MODE_CHARGING:
							TToast.show("模式：计费模式");
							break;
						case MODE_FREE:
							TToast.show("模式：免费模式");
							break;
						default:
							break;
						}
					super.onUIThread(data, params);
				}
			});
		}
		return resutl;
	}

	public void destory() {
		getColdWaterTask().stop();
		getWarmWaterTask().stop();
		getChargingTask().stop();
		mColdWaterTask = null;
		mColdWaterTask = null;
		mWarmWaterTask = null;
		mInstance = null;
	}

	public WaterTask getColdWaterTask() {
		if (U.isNull(mColdWaterTask)) {
			mColdWaterTask = new WaterTask() {

				@Override
				public void openWater() {
					MachineManager.getInstance().openColdWater();
					// TToast.show("Cold Water");
				}

				@Override
				public void closeWater() {
					MachineManager.getInstance().closeColdWater();
				}

				@Override
				protected int onStart(int key,int total) {
					getWarmWaterTask().stop();
					L.i("ColdWaterTask start");
					return super.onStart(key, total);
				}

				@Override
				protected void onStop(int progress, long total) {
					L.i("ColdWaterTask stop");

				}

			};
		}
		return mColdWaterTask;
	}

	public WaterTask getWarmWaterTask() {
		if (U.isNull(mWarmWaterTask)) {
			mWarmWaterTask = new WaterTask() {

				@Override
				public void openWater() {
					MachineManager.getInstance().openWarmWater();
					L.i("WarmWaterTask openWater");
				}

				@Override
				public void closeWater() {
					MachineManager.getInstance().closeWarmWater();
					L.i("WarmWaterTask closeWater");
				}

				@Override
				protected int onStart(int key,int total) {
					getColdWaterTask().stop();
					L.i("WarmWaterTask start");
					return super.onStart(key, total);
				}

				@Override
				protected void onStop(int progress, long total) {
					L.i("WarmWaterTask stop");
				}

			};
		}
		return mWarmWaterTask;
	}

	private ChargingTask getChargingTask() {
		if (U.isNull(mChargingTask)) {
			mChargingTask = new ChargingTask();
		}
		return mChargingTask;
	}

	private class ChargingTask extends WaterTask {

		private int mKey;

		private boolean mStart;

		private boolean mPause;

		private int mProgress;
		
		private int mTotal;
		
		@Override
		public void reset() {
			mKey = 0;			
			mStart = false;
			mPause = false;
			mProgress = 1;
			mTotal =1;
			super.reset();
		}

		@Override
		public void openWater() {
			switch (mKey) {
			case KeyEvent.KEYCODE_VOLUME_UP:
				MachineManager.getInstance().openWarmWater();
				L.i("openWarmWater");
				break;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				MachineManager.getInstance().openColdWater();
				L.i("openColdWater");
				break;
			default:
				break;
			}

		}

		@Override
		public void closeWater() {
			switch (mKey) {
			case KeyEvent.KEYCODE_VOLUME_UP:
				MachineManager.getInstance().closeWarmWater();
				L.i("closeWarmWater");
				break;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				MachineManager.getInstance().closeColdWater();
				L.i("closeColdWater");
				break;
			default:
				break;
			}

		}

		@Override
		protected int onStart(int key,int total) {
			getCountDownTask().stop();
			mKey = key;
			mPause = false;
			mTotal = total;
			if (mStart) {
				return mProgress;
			} else {
				mStart = true;
				EventBus.getDefault().post(new Event.StartWater());
				EventBus.getDefault().post(new Event.ShowAction());
				return 1;
			}

		}

		@Override
		protected boolean onKeyEvent(int key) {
			if (!mStart)
				return false;
			if (mPause)
				return false;
			if (mKey == key) {
				return false;
			} else {
				switch (key) {
				case KeyEvent.KEYCODE_VOLUME_UP:
					MachineManager.getInstance().closeColdWater();
					L.i("closeColdWater");
					MachineManager.getInstance().openWarmWater();
					L.i("openWarmWater");
					break;
				case KeyEvent.KEYCODE_VOLUME_DOWN:
					MachineManager.getInstance().openColdWater();
					L.i("openColdWater");
					MachineManager.getInstance().closeWarmWater();
					L.i("closeWarmWater");
					break;
				default:
					break;
				}
				mKey = key;
				return true;
			}

		}

		@Override
		protected void onStop(int progress, long total) {
			if (progress != total) {
				
				mPause = true;
				mProgress = progress;
				L.i("onStop " + progress);
				// 开始10秒倒计时
				getCountDownTask().start();
			} else {
				
				//完成全量
				Event.CloseWater event = new Event.CloseWater();
				event.realaccount = mVolume;
				EventBus.getDefault().post(event);
				mStart = false;
				mPause = false;
				mProgress = 1;
				mTotal = 1;
				mChargingStart = false;
				L.i("Stop");
				EventBus.getDefault().post(new Event.HideTips());
			}

		}

		public void cancel() {
			L.i("cancel");
			//不全量
			if(mStart && mTotal > 0){
				Event.CloseWater event = new Event.CloseWater();
				event.realaccount = mVolume * mProgress / mTotal;
				EventBus.getDefault().post(event);
			}else{
				unlockTask();
			}			
			mStart = false;
			mPause = false;
			mProgress = 1;
			mTotal = 1;
			mChargingStart = false;
			stop();
			EventBus.getDefault().post(new Event.HideTips());
		}
	}
	
	private void unlockTask(){
		Event.UnlockWater event = new Event.UnlockWater();
		EventBus.getDefault().post(event);
	}

	private CountDownTask getCountDownTask() {
		if (U.isNull(mCountDownTask)) {
			mCountDownTask = new CountDownTask();
		}
		return mCountDownTask;
	}

	private class CountDownTask extends SimpleTask {

		private boolean start;
		private boolean over;

		@Override
		public Object onBGThread(Object... params) throws Exception {

			for (int i = 0; i < CANCEL_TIME; i++) {
				Sleeper.sleep(100);
				if (!start)
					break;
			}
			if (over) {
				getChargingTask().cancel();
			}
			start = false;
			return null;
		}
		
		public void reset(){
			over = false;
			start = false;
		}

		public void stop() {
			over = false;
			start = false;
		}

		public void start() {
			if (!start) {
				start = true;
				over = true;
				TaskManager.getInstance().async(this);
			}
		}
	}
}
